//
//  BiCarSaleNetEngine.m
//  TrunkIndustryManageSystem
//
//  Created by 李璐西 on 15-4-6.
//  Copyright (c) 2015年 李璐西. All rights reserved.
//

#import "BiCarSaleNetEngine.h"
#import "BiNetEngine.h"
#import "ASIFormDataRequest.h"
#import "BiCarSaleParser.h"
#import "ASINetworkQueue.h"

@implementation BiCarSaleNetEngine
SINGLE_INSTANCE_IMPLEMENTION(BiCarSaleNetEngine)

/**
 *   请求整车销售列表，index:0 下拉刷新请求更多，index为起始位置
 */
- (void)requestCarSaleCheckList:(id)delegate index:(int)index
{
    NSString *url = [NSString stringWithFormat:@"%@test/", SERVICE_URL];
    NSDictionary * paramDic = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:index], @"index", nil];
    
    [_dataDic setObject:delegate forKey:[NSNumber numberWithInt:NET_GET_CAR_SALE_CHECK_LIST]];
    
    [self createGetRequest:url paramDic:paramDic tag:NET_GET_CAR_SALE_CHECK_LIST];
    
}

- (void)requestDone:(ASIHTTPRequest *)request
{
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    int httpCode = [request responseStatusCode];
    
    if (200 > httpCode || 300 <= httpCode)
    {
        NSLog(@"http error --- tag = %d, http code = %d", tag, httpCode);
        assert(0);
        return;
    }
    
    switch (tag)
    {
        case NET_GET_CAR_SALE_CHECK_LIST:
        {
            [self onGetCarSaleCheckListData:request];
        }
            break;
        default:
            break;
    }
    
}

- (void)onGetCarSaleCheckListData:(ASIHTTPRequest *)request
{
    int httpCode = [request responseStatusCode];
    
    if (200 != httpCode)
    {
        return;
    }
    
    int tag = [[[request userInfo] objectForKey:@"t_tag"] intValue];
    
    id delegate = [_dataDic objectForKey:[NSNumber numberWithInt:tag]];
    [_dataDic removeObjectForKey:[NSNumber numberWithInt:tag]];  // 请求完后清除掉delegate
    
    if (nil != delegate && [delegate respondsToSelector:@selector(onNetEngineResponse:entity:userInfo:)])
    {
        NSArray * array = [BiCarSaleParser parserCarSaleCheckListEntity:[request responseString]];
        [delegate onNetEngineResponse:tag entity:array userInfo:nil];
    }
}

@end
